var param = JSON.parse(localStorage.getItem('param'));
var productId = param.productId;
var userId = localStorage.getItem('userid');
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	//初始化商品信息
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/product/getTeaProductInfo.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			productId:productId,
			userId:userId
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				var prodPic = result.data.productPic;
				var prod = result.data;
				if(prodPic){
					//获取到图片对象
					var coverPic = prodPic.coverPic;//封面图
					var coverPics = coverPic.split(",");
					var upStr = '';
					$.each(coverPics,function(i,n){
						upStr += '<div class="swiper-slide"><img src="'+n+'" /></div>';
					});
					$('.swiper-wrapper').html(upStr);
					
					var pics = getPics(prodPic);
					var downStr = '<div class="comment-normal"><span>详情</span></div>';
					$.each(pics,function(i,n){
						downStr += '<div class="img-detail"><img src="'+n+'" alt="" /></div>';
					});
					$('#proDetail').html(downStr);
					
					/*var picsStr = prodPic.zgcyPic;
					var pics = picsStr.split(',');
					var upStr = '';
					var downStr = '<div class="comment-normal"><span>详情</span></div>';
					$.each(pics,function(i,n){
						upStr += '<div class="swiper-slide"><img src="'+n+'" /></div>';
						downStr += '<div class="img-detail"><img src="'+n+'" alt="" /></div>';
					});
					$('.swiper-wrapper').html(upStr);
					$('#proDetail').html(downStr);*/
				}
				var str = '<p class="pro-name">'+prod.teaTitle+'</p><div class="item-time clear"><span class="fl">林地面积：'+prod.teaArea+'亩 </span><span class="fr">海拔高度：'+prod.teaHight+'米</span></div><div class="item-time clear"><span class="fl">建房用地面积：'+prod.coverArea+'平方米</span></div>';
				$('.skill-box').html(str);
				
				var gandenTypeStr = '';
				if(prod.gandenType==0){
					gandenTypeStr = "国家生态林";
				}else if(prod.gandenType==1){
					gandenTypeStr = "成型茶园";
				}else if(prod.gandenType==2){
					gandenTypeStr = "荒山茶林";
				}
				
				var teaTypeStr = '';
				var $teaType = prod.teaType;
				var teaTypes = $teaType.split(",");
				$.each(teaTypes,function(i,n){
					if(n==0){
						teaTypeStr += "绿茶、";
					}else if(n==1){
						teaTypeStr += "青茶、";
					}else if(n==2){
						teaTypeStr += "白茶、";
					}else if(n==3){
						teaTypeStr += "红茶、";
					}else if(n==4){
						teaTypeStr += "黑茶、";
					}else if(n==5){
						teaTypeStr += "黄茶、";
					}
				});
				teaTypeStr = teaTypeStr.substring(0,teaTypeStr.length-1);
				
				var detailStr = ' <div class="comment-normal"><span>详细信息</span></div><div class="det-inf"><span>林地详细地址：'+prod.detailAddress+'</span></div><div class="det-inf"><span>林权证明有效期：'+prod.limitTime+'年</span></div><div class="det-inf"><span>林地价格：'+prod.teaPrice+'元/亩·年</span></div><div class="det-inf"><span>林地性质：'+gandenTypeStr+'</span></div><div class="det-inf"><span>已有茶树：'+prod.treeNum+'棵</span></div><div class="det-inf"><span>可制作成品茶种类：'+teaTypeStr+'</span></div><div class="det-inf"><span>其他设施说明：'+prod.teaNotice+'</span></div>';
				$('#prod').html(detailStr);
				
				var mySwiper1 = new Swiper('.swiper-container2', {
					direction: 'horizontal',
					loop: true,
				});
			}else{
				showErrorMsg(result.data);
			}
		}
	});
	//查看用户是否点赞该商品
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/user/collect/checkCollect.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			userId:userId,
			productId:productId
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				if(result.data){
					$('#teaCollect').addClass("collect-sel");
					$('.comment-btn-z').attr('collectId',result.data);
				}
			}else{
				showErrorMsg(result.data);
			}
		}
	});
}
//初始化绑定事件
function initButton(){
	/*$("body").on('click','.comment-btn',function(){
		localStorage.setItem('orderId',productId);
		window.location = server_url + "allComment.do";
	});*/
	/*$("body").on('click','.comment-btn-l',function(){
		localStorage.setItem('id',productId);
		window.location = server_url + "comment1.do";
	});*/
	$('#chat').on('click',function(){
		window.location = server_url + "tea/views/chat/chat.html";
	});
	$('.comment-btn-z').on('click', function() {
		$(this).children('p').toggleClass('collect-sel');
		var collect = $(this);
		var collectId = $(this).attr('collectid');
		if(!(collectId=="")){
			//原先是收藏的，现在点是为了取消收藏
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/user/collect/deleteCollect.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					id:collectId
				},
				success : function(result) {
					collect.attr('collectid','');
				}
			});
		}else{
			//原先没有收藏现在是在收藏
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/user/collect/addCollect.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					productId:productId,
					userId:localStorage.getItem('userid')
				},
				success : function(result) {
					collect.attr('collectid',result.data.id);
				}
			});
		}
	});
	
	/*//绑定点赞
	$('body').on('click','.comment-btn-z', function() {
		$(this).children('p').toggleClass('comment-btn-sel');
		var favour = $(this);
		var markId = $(this).attr('markid');
		if(!(markId=="")){
			//原先是点过赞的，现在点是为了取消点赞
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/tea/favour/deleteFavour.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					id:markId
				},
				success : function(result) {
					favour.attr('markid','');
				}
			});
		}else{
			//原先没有点赞现在是在点赞
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/tea/favour/addFavour.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					markId:productId,
					userId:userId
				},
				success : function(result) {
					favour.attr('markid',result.data.id);
				}
			});
		}
	})*/
}